package com.hj.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hj.reggie.common.R;
import com.hj.reggie.domain.Category;
import com.hj.reggie.domain.Dish;
import com.hj.reggie.dto.DishDto;
import com.hj.reggie.service.CategoryService;
import com.hj.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品管理
 *
 * @author 37460
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    //spring数据注入的时候建议构造函数注入
    private final DishService dishService;

    private final CategoryService categoryService;


    public DishController(DishService dishService, CategoryService categoryService) {
        this.dishService = dishService;
        this.categoryService = categoryService;

    }

    /**
     * 保存菜品和口味
     *
     * @param dishDto 提交的菜品和口味数据
     * @return 成功
     */
    @PostMapping()
    public R<String> save(@RequestBody DishDto dishDto) {
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    /**
     * 修改菜品和口味
     *
     * @param dishDto 提交的菜品和口味数据
     * @return 成功
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateDishWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }

    /**
     * 查询菜品
     *
     * @param page     传入的页数
     * @param pageSize 显示得条数
     * @param name     要查询的菜品 可选值
     * @return 返回分页的数据  dishDto
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {

        //构造分页构造器
        Page<Dish> dishPage = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);
        //执行分页查询
        dishService.page(dishPage, new LambdaQueryWrapper<Dish>()
                //如果name不为空执行模糊查询
                .like(name != null, Dish::getName, name)
                //添排序条件按照更新时间进行降序
                .orderByDesc(Dish::getUpdateTime));
        //对象copy 把dishPage拷贝到dishDtoPage上面去忽略records集合这个需要数据处理
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");

        //处理dishRecords数据生成DishDto集合
        List<Dish> dishRecords = dishPage.getRecords();
        List<DishDto> dishDtoList = dishRecords.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            //拷贝dish对象得属性到dishDto上面去
            BeanUtils.copyProperties(dish, dishDto);
            //获取分类id
            Long categoryId = dish.getCategoryId();
            //根据id查询对象
            Category category = categoryService.getById(categoryId);
            if (categoryId != null) {
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());
        //把处理好的records在塞进  dishDtoPage中去
        dishDtoPage.setRecords(dishDtoList);
        return R.success(dishDtoPage);
    }

    /**
     * 单独删除菜品
     *
     * @param ids 菜品id集合
     * @return 返回成功
     */
    @DeleteMapping
    public R<String> deleteDish(Long[] ids) {
        //处理ids生成dish对象集合
        List<Dish> dishList = Arrays.stream(ids).map(item -> {
            Dish dish = new Dish();
            dish.setId(item);
            return dish;
        }).collect(Collectors.toList());
        dishService.removeBatchByIds(dishList);
        return R.success("删除成功");
    }

    /**
     * @param id 根据id查询菜品和口味给页面回显数据
     * @return 返回成功
     */
    @GetMapping("{id}")
    public R<DishDto> getByIdDishWithFlavor(@PathVariable Long id) {
        DishDto byIdDishWithFlavor = dishService.getByIdDishWithFlavor(id);
        return R.success(byIdDishWithFlavor);
    }

    /**
     * 批量停售和启售菜品方法
     *
     * @param ids 菜品id
     * @return 状态
     */
    @PostMapping("/status/{state}")
    public R<String> switchSalesStatus(@PathVariable Integer state, Long[] ids) {
        log.info("state : {}", state);
        log.info(ids.toString());
        //执行更新dish state字段
        dishService.switchSalesStatus(state, ids);
        return R.success("修改成功");
    }

    /**
     * 根据categoryId 查询所有相关的菜品
     * @param dish dish
     * @return 返回菜品的dish集合
     */
    @GetMapping("/list")
    public R<List<Dish>> getList(Dish dish) {
        //构造条件查询
        List<Dish> list = dishService.list(new LambdaQueryWrapper<Dish>()
                //根据CategoryId查询
                .eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId())
                //查询状态为1 1起售状状态
                .eq(Dish::getStatus,1)
                //排序方式
                .orderByAsc(Dish::getSort)
                .orderByDesc(Dish::getUpdateTime));
        return R.success(list);
    }
}
